package com.ymttest.business.data;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.shop2cn.iapi.sellerpromotion.parameter.PromotionDetailBean;
import com.shop2cn.iapi.sellerpromotion.service.PromotionDetailCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.dguser.parameter.BindBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.parameter.UserDto;
import com.ymatou.iapi.dguser.service.BindCall;
import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.service.GetProductInfoByProductIdCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.Shop2cnSellerPromotionCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.mongo.Shop2cnSellerPromotionInfoWapper;
import com.ymttest.utils.TestDataManager;

public class SqData {
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Test");
		try {
		} catch (Exception e) {
		}
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("test");
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateToken_001() {
		Logger.start(true, "stg做测试数据-更新token");
		TestDataManager.getLines("squser.txt").forEach(line->{
			String[] ss = line.split(",");
			Integer user=Integer.valueOf(ss[0]);
			String token="0";
			try {
				token = AuthCallServiceV2.createToken(user, "62");
			} catch (IOException e) {
				e.printStackTrace();
			}
			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(user);
			TestDataManager.appendFileToLine("squser2.txt", user + "," + token + "," + ss[2]);
		});
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateUser_001() {
		Logger.start(true, "stg做测试数据-创建平台用户");
		for (int i = 0; i < 1000; i++) {
			try {
				UserCreateBean u = DGUserCallService.initUsercreateBean();
				u.setMobile("87" + u.getMobile());
				int user = DGUserCallService.UserCreateCall(u);
				UserDto dgUserInfo = DGUserCallService.getDGUserInfo(user);
				System.out.println(dgUserInfo);
				BindBean bindBean = new BindBean();
				BindCall bindCall = new BindCall();
				bindBean.setUserId(user);
				bindBean.setAppSource(62);
				bindBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
				bindBean.setUnionId(u.getUnionId());
				bindBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
				bindCall.setData(bindBean);
				bindCall.callService();
//				Logger.verifyEquals("true", bindCall.getString("success"), "验证success");
				dgUserInfo = DGUserCallService.getDGUserInfo(user);
				System.out.println(dgUserInfo);
				String token = AuthCallServiceV2.createToken(user, "62");
				AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(user);
				String addressid = JUserCallService.AddressRequestDtoCall(addressBean);
				TestDataManager.appendFileToLine("squser.txt", user + "," + token + "," + addressid);
			} catch (Exception e) {
				Logger.fail(e);
			} catch (Throwable throwable) {
				throwable.printStackTrace();
			}
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateProduct_001() {
		Logger.start(true, "stg做测试数据-创建商品");
		try {
			List<String> productlist = new ArrayList<>();
			//stg
//			int sellerId = 500003891;
			//sit
			int sellerId=500041475;
			for (int i = 0; i < 20; i++) {
				try {
					AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
							"autotest", false);
					while (addAgentProductBean.getCatalogList().size() > 1) {
						addAgentProductBean.getCatalogList().remove(0);
					}
					addAgentProductBean.getCatalogList().get(0).setVirtualStock(999999);
					addAgentProductBean.getCatalogList().get(0).setStock(999999);
					addAgentProductBean.getCatalogList().get(0).getAgentPriceList().forEach(b -> {
						b.setAgentPrice(2d);
					});
					addAgentProductBean.getCatalogList().get(0).setDirectPrice(2d);
					addAgentProductBean.setCurrency("CNY");
					String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
					productlist.add(productId);
				} catch (Exception e) {
				}
			}
			Thread.sleep(1000);
			productlist.forEach(productId -> {
				JSONObject prodDetail = new GetProductInfoByProductIdCall().callAndGetReturnData(productId);
				ProductInDetailBean prodBean = null;
				List<String> catalogList = new ArrayList<>();
				try {
					prodBean = ProductQueryCallService.getBeanFromJson(
							prodDetail.getJSONObject("Data").getJSONObject("Product"), ProductInDetailBean.class);
					catalogList = prodBean.getCatalogList().stream().map(x -> x.getCatalogId())
							.collect(Collectors.toList());
				} catch (Throwable e) {
					try {
						Thread.sleep(1000);
						prodBean = ProductQueryCallService.getBeanFromJson(
								prodDetail.getJSONObject("Data").getJSONObject("Product"), ProductInDetailBean.class);
						catalogList = prodBean.getCatalogList().stream().map(x -> x.getCatalogId())
								.collect(Collectors.toList());
					} catch (Exception e1) {
						e1.printStackTrace();
					}

				}

				for (String catalog : catalogList) {
					TestDataManager.appendFileToLine("sqproduct_sit.txt", sellerId + "," + productId + "," + catalog);
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReceiveCoupon_001() {
		Logger.start(true, "stg做测试数据-领取优惠券");
		int userid=500002381;
		int SellerId=500003891;
		SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
		sellerCreateBatchBean.setSellerId(SellerId);
		sellerCreateBatchBean.setCouponAmount(new BigDecimal(1));
		sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(2));
		sellerCreateBatchBean.setCouponTotalCount(1000);
		sellerCreateBatchBean.setReceiveNumPerUser(1000);
		SellerCreateBatchCall sellerCreateBatchCall = null;
		try {
			sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean,false);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String batchCode=sellerCreateBatchCall.getBatchCode();
		for (int i = 0; i < 1000; i++) {
			
			
			try {
				String cc=CouponShop2cnMCallService.receiveCouponInfo(batchCode, userid).getUserReceiveResult().getCouponCode();
				TestDataManager.appendFileToLine("ReceiveCoupon.txt", userid + "," + cc );
				Thread.sleep(15);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
//		TestDataManager.getLines("squser.txt").forEach(line->{
//			String[] ss = line.split(",");
//			Integer user=Integer.valueOf(ss[0]);
//			String token="0";
//			try {
//				token = AuthCallServiceV2.createToken(user, "62");
//				
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(user);
//			TestDataManager.appendFileToLine("squser2.txt", user + "," + token + "," + ss[2]);
//		});
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateCoupon_002() {
		Logger.start(true, "stg做测试数据-更新coupon-带openid");
		int SellerId=500003891;
		TestDataManager.getLines("openid_user.txt").forEach(line->{
			System.out.println(line);
			String[] ss = line.split(",");
			Integer user=Integer.valueOf(ss[0]);
			String token=ss[1];
			String addressid=ss[2];
			String openid=ss[3];
			
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean.setSellerId(SellerId);
			sellerCreateBatchBean.setCouponAmount(new BigDecimal(1));
			sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(2));
			sellerCreateBatchBean.setCouponTotalCount(1000000);
			sellerCreateBatchBean.setReceiveNumPerUser(1000);
			SellerCreateBatchCall sellerCreateBatchCall = null;
			try {
				sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean,false);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			String batchCode=sellerCreateBatchCall.getBatchCode();
			for (int i = 0; i < 1000; i++) {
				try {
					String cc=CouponShop2cnMCallService.receiveCouponInfo(batchCode, user).getUserReceiveResult().getCouponCode();
//					TestDataManager.appendFileToLine("ReceiveCoupon.txt", user + "," + cc );
					TestDataManager.appendFileToLine("openid_user3.txt", user + "," + token + "," +addressid+ "," +openid+ "," +cc);
					Thread.sleep(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	});
	}
//	@Test
//	@Category({ P1.class })
//	@TestCase
//	public void Tc_UpdateCoupon_003() {
//		System.out.println(TestDataManager.getCardId(30, 1));
//	}
}
